<template>
  <div class="app-container">

    <!--工具栏-->
    <div class="head-container" v-if="currentPage === 1">
      <div v-if="crud.props.searchToggle">
        <el-input v-model="query.name" clearable size="small" placeholder="输入名称搜索" style="width: 200px;"
                  class="filter-item" @keyup.enter.native="crud.toQuery"/>
        <rrOperation/>
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <!--      <crudOperation :permission="permission" />-->
      <crudOperation :permission="permission">
        <template slot="right">
          <el-button
            class="filter-item"
            size="mini"
            type="success"
            @click="pictureManagement"
          >
            图片管理
          </el-button>
          <el-button
            class="filter-item"
            size="mini"
            type="success"
            @click="directoryStructure"
          >
            目录结构
          </el-button>
        </template>
      </crudOperation>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0"
                 :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="ISBN" prop="isbn">
            <el-input v-model="form.isbn" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="分类" prop="classificationId">
            <el-input v-model="form.classificationId" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="语言" >
            <el-input v-model="form.dictionaryId" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="名称(蒙)" prop="mnName">
            <el-input v-model="form.mnName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="名称(汉)" prop="zhName">
            <el-input v-model="form.zhName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="名称(拉丁)" prop="ldName">
            <el-input v-model="form.ldName" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="作者" prop="author">
            <el-input v-model="form.author" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="出版者" >
            <el-input v-model="form.publisher" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="出版地" >
            <el-input v-model="form.placePublication" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="印刷者" >
            <el-input v-model="form.printer" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="来源" >
            <el-input v-model="form.source" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="开本" >
            <el-input v-model="form.format" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="简介" >
            <el-input v-model="form.intro" type="textarea" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.remark" style="width: 370px;"/>
          </el-form-item>
          <el-form-item label="状态">
            <el-switch
              :active-value="1"
              :inactive-value="0"
              v-model="form.status"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;"
                @selection-change="crud.selectionChangeHandler" @select="selectTtem">
        <el-table-column type="selection" width="55"/>
        <el-table-column prop="id" label="id"/>
        <el-table-column prop="isbn" label="ISBN"/>
        <el-table-column prop="classificationId" label="分类"/>
        <el-table-column prop="dictionaryId" label="语言"/>
        <el-table-column prop="mnName" label="名称(蒙)"/>
        <el-table-column prop="zhName" label="名称(汉)"/>
        <el-table-column prop="ldName" label="名称(拉丁)"/>
        <el-table-column prop="author" label="作者"/>
        <el-table-column prop="publisher" label="出版者"/>
        <el-table-column prop="placePublication" label="出版地"/>
        <el-table-column prop="printer" label="印刷者"/>
        <el-table-column prop="source" label="来源"/>
        <el-table-column prop="format" label="开本"/>
        <el-table-column prop="intro" label="简介"/>
        <el-table-column prop="createBy" label="创建者"/>
        <el-table-column prop="updateBy" label="修改者"/>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-switch
              :active-value="1"
              :inactive-value="0"
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              changeEnabled
              @change="changeState(scope.row,scope.row.status)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column v-if="checkPer(['admin','bookDocuments:edit','bookDocuments:del'])" label="操作" width="150px"
                         align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination/>
    </div>

    <div v-else-if="currentPage === 2">
      <DocPicture :selection="this.selection" @update-value="updateValue"></DocPicture>
    </div>

    <div v-else-if="currentPage === 3">
      <Catalogue :selection="this.selection" @update-value="updateValue"></Catalogue>
    </div>
  </div>
</template>
<script>
import crudBookDocuments from '@/api/literatureManagement/bookDocuments'
import CRUD, {crud, form, header, presenter,} from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import Catalogue from '@/views/literatureManagement/catalogue/index.vue'
import DocPicture from '@/views/literatureManagement/docPicture/index.vue'

const defaultForm = {
  id: null,
  isbn: null,
  classificationId: null,
  dictionaryId: null,
  mnName: null,
  nameCode: null,
  zhName: null,
  ldName: null,
  author: null,
  publisher: null,
  placePublication: null,
  printer: null,
  source: null,
  format: null,
  intro: null,
  createBy: null,
  createTime: null,
  updateBy: null,
  updateTime: null,
  remark: null,
  status: null
}
export default {
  name: 'BookDocuments',
  components: {pagination, crudOperation, rrOperation, udOperation, DocPicture, Catalogue},
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '书籍文献管理',
      url: 'api/bookDocuments',
      idField: 'id',
      sort: 'id,desc',
      crudMethod: {...crudBookDocuments}
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'bookDocuments:add'],
        edit: ['admin', 'bookDocuments:edit'],
        del: ['admin', 'bookDocuments:del']
      },
      rules: {
        isbn: [
          {required: true, message: '请输入ISBN', trigger: 'change'}
        ],
        classificationId: [
          {required: true, message: '请输入分类', trigger: 'change'}
        ],
        dictionaryId: [
          {required: true, message: '请输入内容语言', trigger: 'change'}
        ],
        mnName: [
          {required: true, message: '请输入名称(蒙)', trigger: 'change'}
        ],
        zhName: [
          {required: true, message: '请输入名称(汉)', trigger: 'change'}
        ],
        ldName: [
          {required: true, message: '请输入名称(拉丁)', trigger: 'change'}
        ],
        author: [
          {required: true, message: '请输入作者', trigger: 'change'}
        ],
        publisher: [
          {required: true, message: '请输入出版者', trigger: 'change'}
        ],
        placePublication: [
          {required: true, message: '请输入出版社', trigger: 'change'}
        ],
        printer: [
          {required: true, message: '请输入印刷者', trigger: 'change'}
        ],
        source: [
          {required: true, message: '请输入来源', trigger: 'change'}
        ],
        format: [
          {required: true, message: '请输入开本', trigger: 'change'}
        ],
        intro: [
          {required: true, message: '请输入简介', trigger: 'change'}
        ]
      },
      selections: [],
      selection: null,
      currentPage: 1,
      dialogVisible: false,
      fileList: [],
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    changeState(data, val) {
      this.$confirm('此操作将修改状态值为 ' + val + ' 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        crudBookDocuments.edit(data).then(res => {
          this.$message({
            type: 'waring',
            message: '修改成功'
          });
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消修改'
        });
      });
    },
    pictureManagement() {
      if (this.selections.length === 0) {
        this.$message('请选择一条记录');
      }
      if (this.selections.length > 1) {
        this.$message('最多选择一条记录');
      }
      if (this.selections.length === 1) {
        this.currentPage = 2;
      }
    },
    selectTtem(selection, row) {
      this.selections = selection;
      this.selection = row;
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {
        });
    },
    updateValue(newValue) {
      this.currentPage = newValue;
    },
    directoryStructure() {
      if (this.selections.length === 0) {
        this.$message('请选择一条记录');
      }
      if (this.selections.length > 1) {
        this.$message('最多选择一条记录');
      }
      if (this.selections.length === 1) {
        this.currentPage = 3;
      }
    }
  }
}
</script>

<style scoped>

</style>
